package com.zc.edu.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zc.edu.client.OrderClient;
import com.zc.edu.entity.Course;
import com.zc.edu.entity.vo.ChapterVo;
import com.zc.edu.entity.vo.front.CourseFrontVo;
import com.zc.edu.service.ChapterService;
import com.zc.edu.service.CourseService;
import com.zc.utils.JwtUtils;
import com.zc.utils.R;
import com.zc.utils.vo.CourseShowInfo;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/eduService/frontCourse")
@CrossOrigin
public class CourseFrontController {

	@Autowired
	private CourseService courseService;

	@Autowired
	private ChapterService chapterService;

	@Autowired
	private OrderClient orderClient;

	// 分页查询讲师
	@ApiOperation("前台分页查询讲师")
	@PostMapping("/pageCourse/{currentPage}/{size}")
	public R pageListCourse(@ApiParam(name = "currentPage", value = "当前页")@PathVariable("currentPage")long currentPage,
	                         @ApiParam(name = "size", value = "每页记录数")@PathVariable("size")long size,
	                         @ApiParam(name="CourseFrontVo",value = "查询条件") @RequestBody(required = false) CourseFrontVo courseFrontVo) {
		Page<Course> pageCourse = new Page<>(currentPage, size);
		Map<String, Object> pageInfo =  courseService.getCourseList(pageCourse, courseFrontVo);
		return R.ok().data(pageInfo);
	}

	// 根据课程Id查询课程信息
	@GetMapping("/getCourseInfo/{id}")
	public R getCourseInfo(@PathVariable String id, HttpServletRequest request) {
		// 查询课程展示信息
		CourseShowInfo courseShowInfo = courseService.getFrontCourseInfo(id);

		// 查询课程章节和小节
		List<ChapterVo> allChapter = chapterService.getAllChapter(id);
		String memberId = JwtUtils.getMemberIdByJwtToken(request);
		boolean isBuy = orderClient.queryCourseIsBuyed(id, memberId);

		return R.ok().data("courseShowInfo", courseShowInfo).data("allChapter", allChapter)
				.data("isBuy", isBuy);
	}

	//根据课程id查询课程信息
	@GetMapping("/getCourseInfoOrder/{id}")
	public CourseShowInfo getCourseInfoOrder(@PathVariable String id) {
		return courseService.getFrontCourseInfo(id);
	}

}
